package com.cncf.service.impl;

import com.cncf.dao.ReportMapper;
import com.cncf.entity.Report;
import com.cncf.entity.ReportExample;
import com.cncf.service.ReportService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author zj
 */
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportMapper reportMapper;

    @Override
    public boolean insertReportForBatch(String headerString, InputStream is) throws IOException {
        List<Report> reportList = new ArrayList<>();
        Workbook workbook = null;
        if (headerString.endsWith(".xls")) {
            System.out.println("excel后缀为+“.xls”");
            workbook = new HSSFWorkbook(is);
        }
        if (headerString.endsWith(".xlsx")) {
            System.out.println("excel后缀为+“.xlsx”");
            workbook = new XSSFWorkbook(is);
        }
        Sheet sheet = workbook.getSheetAt(0);
        Iterator<Row> iterator = sheet.iterator();
        //这个if块是为了去掉每个sheet的标题行，之所以要判断是否存在后继元素是因为：
        //读到空的sheet时如果直接执行next()方法会报NoSuchElementException异常.
        //顺便用标题行来判断列数，且必须用标题行来判断
        int colNumber = 0;
        if (iterator.hasNext()) {
            Row row = iterator.next();
            colNumber = row.getPhysicalNumberOfCells();
            if (colNumber != 13) {//有记录但列数不为13
                return false;
            }
        } else {//一行记录都没有，连标题都没有
            return false;
        }

        int rowCount = 0;
        while (iterator.hasNext()) {//这层循环遍历sheet的行
            Row row = iterator.next();
            rowCount++;
            Report report = new Report();

            Cell cell = row.getCell(0);
            if (cell == null) {
                throw new RuntimeException("第" + rowCount + "条记录的第一列不能为空");
            }
            cell.setCellType(Cell.CELL_TYPE_STRING);
            report.setId(cell.getStringCellValue());

            cell = row.getCell(1);
            if (cell != null) {
                cell.setCellType(Cell.CELL_TYPE_STRING);
            }
            report.setReportNumber(cell == null ? "" : cell.getStringCellValue());

            cell = row.getCell(2);
            if (cell != null) {
                cell.setCellType(Cell.CELL_TYPE_STRING);
            }
            report.setProductId(cell == null ? "" : cell.getStringCellValue());

            cell = row.getCell(3);
            if (cell != null) {
                cell.setCellType(Cell.CELL_TYPE_STRING);
            }
            report.setCheckTypeName(cell == null ? "" : cell.getStringCellValue());

            cell = row.getCell(4);
            if (cell != null) {
                cell.setCellType(Cell.CELL_TYPE_STRING);
            }
            report.setSpecification(cell == null ? "" : cell.getStringCellValue());

            cell = row.getCell(5);
            if (cell != null) {
                cell.setCellType(Cell.CELL_TYPE_STRING);
            }
            report.setCheckBasis(cell == null ? "" : cell.getStringCellValue());

            cell = row.getCell(6);
            if (cell != null) {
                cell.setCellType(Cell.CELL_TYPE_STRING);
            }
            report.setSampleLevel(cell == null ? "" : cell.getStringCellValue());

            cell = row.getCell(7);
            if (cell != null) {
                cell.setCellType(Cell.CELL_TYPE_STRING);
            }
            report.setCheckItem(cell == null ? "" : cell.getStringCellValue());

            cell = row.getCell(8);
            if (cell != null) {
                cell.setCellType(Cell.CELL_TYPE_STRING);
            }
            report.setTestResult(cell == null ? "" : cell.getStringCellValue());
            System.out.println("testResult:" + report.getTestResult());

            cell = row.getCell(9);
            if (cell != null) {
                cell.setCellType(Cell.CELL_TYPE_NUMERIC);
            }
            report.setSignatureDate(cell == null ? null : cell.getDateCellValue());

            cell = row.getCell(10);
            String newUrl="";
            if (cell != null) {
                cell.setCellType(Cell.CELL_TYPE_STRING);
                String reg = ".*\\/\\/([^\\/\\:]*).*";
                String oldUrl=cell.getStringCellValue();
                System.out.println(oldUrl);
                String oldHost=oldUrl.replaceAll (reg, "$1");
                String newHost="47.93.242.215";
                newUrl=oldUrl.replace(oldHost,newHost);
                System.out.println (newUrl);
            }

            report.setReportFilePath(cell == null ? "" : newUrl);
            reportList.add(report);
        }

        int res = reportMapper.insertReportForBatch(reportList);
        return res == rowCount;
    }

    @Override
    public String getReportFilePathById(String id) {
        Report report = reportMapper.selectByPrimaryKey(id);
        if (report == null) {
            return null;
        }
        return report.getReportFilePath();
    }

    @Override
    public Report getReportById(String id) {
        return reportMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Report> getAllReport(Integer page, Integer pageSize) {
        if (page == null || pageSize == null) {
            ReportExample reportExample = new ReportExample();
            return reportMapper.selectByExample(reportExample);
        }
        int offset = page * pageSize;
        int limit = pageSize;
        //return reportMapper.selectAllReportByPage(offset,limit);
        return new ArrayList<Report>();
    }

}
